package io.github.loan.users.service.impl;


import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import io.github.loan.common.util.PageInfoUtil;
import io.github.loan.users.dao.IAdminsDao;
import io.github.loan.users.pojo.Admins;
import io.github.loan.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService{

	@Resource
	public IAdminsDao adminsDao;
	
	/* -----------------管理员操作开始------------------------- */
	@Override
	public List<Admins> findAdminsCondListService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		List<Admins> adminsList=Collections.EMPTY_LIST;
		if(condMap.get("keyword")!=null){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null){
			condMap.put("pageCount","true");
			condMap.put("currRecord",pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize",pageInfoUtil.getPageSize());
			/*查询总记录数*/
			adminsList=this.adminsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			
			/*查询分页内容*/
			condMap.put("pageCount", "false");
			adminsList=this.adminsDao.findCondList(condMap);
		}else{
			//condMap.put("pageCount","false");
			adminsList=this.adminsDao.findCondList(condMap);
		}
		return adminsList;
	}
	
	@Override
	public Admins findOneAdminsService(Map<String, Object> condMap) {
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneAdminsService(Admins admins) {
		JSONObject resultJSON=new JSONObject();
		/*验证邮箱的唯一，服务器端验证，service验证*/
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("email", admins.getEmail());
		Admins adminsRes=this.findOneAdminsService(condMap);
		if(adminsRes!=null){
			resultJSON.put("code","2");
			resultJSON.put("info", "该邮箱已被使用,请重新输入");
			return resultJSON;
		}
		int res=this.adminsDao.insert(admins);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(Admins admins) {
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.update(admins);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneAdminsService(Admins admins) {
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.delete(admins);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}
	/* -----------------管理员操作结束------------------------- */

	
}
